import cloud from '@lafjs/cloud'
let data
let dbName = 'teaching-archives'
const db = cloud.mongo.db
import { pinyin } from "pinyin-pro";

export default async function (ctx: FunctionContext) {
  data = ctx.body.data
  switch (ctx.body.type) {
    case "add":
      await cloud.utils.addData(dbName, data)
      await setPinyin()
      return cloud.utils.requestReturn({ data: "添加成功" })
      break;
    case "update":
      await cloud.utils.updateData(dbName, data)
      await setPinyin()
      return cloud.utils.requestReturn({ data: "修改成功" })
      break;
    case "del"://删除
      return await delFunc()
      break;
    case "list":
      return await list()
      break;
    case "details":
      return await details()
      break;
    case "bindClass"://绑定班级
      return await cloud.utils.updateData(dbName, data)
      break;
    case "evaluation-record"://关联查询考核评价
      return await evaluationRecord()
      break;
    default:
      break;
  }

  //关联查询考核评价
  async function evaluationRecord() {
    let { lecturerId } = await cloud.nw.db.findByWhereJson({
      dbName, whereJson: { _id: data.archivesId }
    });
    let list = await db.collection("admin-class").find({ "assessmentRecord.assessedTeacherId": lecturerId }).toArray()
    let arr = []
    list.forEach(element => {
      let obj = { ...element.assessmentRecord }
      obj["className"] = element.className
      obj["classId"] = element._id
      obj["classTeacherId"] = element.classTeacherId
      arr.push(obj)
    });
    return cloud.utils.requestReturn({ data: arr })
  }

  //删除该老师相关信息
  async function delFunc() {
    let dbNames = ["archives-curriculum", 'archives-appendix', 'archives-teaching', 'archives-tasks']
    for (let index = 0; index < dbNames.length; index++) {
      const element = dbNames[index];
      await cloud.nw.db.del({
        dbName: element,
        whereJson: { archivesId: data._id, }
      });
    }
    return cloud.utils.deleteData(dbName, data)
  }

  async function details() {
    let resp = await cloud.nw.db.findByWhereJson({
      dbName,
      whereJson: { _id: data._id, },
    });
    let query = await cloud.nw.db.findByWhereJson({
      dbName: "admin-teachers",
      whereJson: { _id: resp.lecturerId, },
    });
    resp.teacher_info = query
    return cloud.utils.requestReturn({ data: resp })
  }

  //设置老师姓名拼音
  async function setPinyin() {
    let array = await db.collection(dbName).find({}).toArray()
    for (let index = 0; index < array.length; index++) {
      const element = array[index];
      if (!element.lecturerNamePinyin) {
        element.lecturerNamePinyin = pinyin(element.lecturerName, { toneType: "none" })
        await cloud.utils.updateData(dbName, element)
      }
    }
  }

  async function list() {
    let whereJson = { ...data }
    delete whereJson.pageIndex
    delete whereJson.pageSize
    whereJson = cloud.utils.fuzzyQuery({ ...whereJson })
    console.log("wwwww", whereJson)
    let res = await cloud.nw.db.selects({
      dbName,
      getCount: true,
      pageIndex: data.pageIndex,
      pageSize: data.pageSize,
      whereJson,
      sortArr: [
        { "name": "teachingStatus", "type": "desc" },
        { "name": "classHour", "type": "asc" },
        { "name": "lecturerNamePinyin", "type": "asc" }
      ],
      foreignDB: [
        {
          dbName: "admin-teachers",//副表 
          localKey: "lecturerId",
          foreignKey: "_id",
          as: "teacher_info",
          limit: 1,
          // 副表字段显示规则
          fieldJson: { lecturerName: 1, mobile: 1, remarks: 1 },
        },
      ]
    });
    return cloud.utils.requestReturn({ data: res })
  }
}
